<html>
<head>
    <meta charset="UTF-8">
    <title>资源管理</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/layer/layer.js"></script>
</head>
<body>
<div id="app">
    <nav class="navbar navbar-inverse" style="border-radius: 0">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">智原资源管理系统</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">

                <ul class="nav navbar-nav">

                    <li class="active">
                        <a href="/index.html">首页</a>
                    </li>
                    <li v-for="d in userResources" v-if="d.pid==0">
                        <a :href="d.url">{{d.name}}</a>
                    </li>

                </ul>
                <ul class="nav navbar-nav navbar-right hidden-sm">
                    <li><a>{{userName}}</a></li>
                    <li><a @click="logout()" href="#">退出</a></li>
                </ul>
            </div>

        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <h1>资源管理</h1>
                <a href="#" type="button" class="btn btn-success" @click="addResource()">添加</a>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12" style="margin-top: 20px">
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true" v-for="r in pageObj">

                    <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingOne">
                            <h4 class="panel-title" style="line-height: 2">
                                <a role="button" data-toggle="collapse" data-parent="#accordion" :href="'#'+ r.name"
                                   aria-expanded="true"> {{r.name}} {{r.url}}

                                    <a href="#" type="button" class="btn btn-warning btn-sm pull-right" @click="updateResource(r.id)">
                                        修改
                                    </a>
                                    <a href="#" type="button" class="btn btn-danger btn-sm pull-right" @click="deleteId(r.id)">
                                        删除
                                    </a>
                                </a>
                            </h4>
                        </div>
                        <div :id="r.name" class="panel-collapse collapse in" role="tabpanel"
                             aria-labelledby="headingOne">
                            <ul class="panel-body">

                                <li class="list-group-item" style="line-height: 2" v-for="s in r.son">{{s.name}}
                                    {{s.url}}
                                    <a href="#" type="button"
                                       class="btn btn-warning btn-sm pull-right" @click="updateResource(s.id)">
                                        修改
                                    </a>
                                    <a href="#" type="button"
                                       class="btn btn-danger btn-sm pull-right" @click="deleteId(s.id)">
                                        删除
                                    </a></li>

                            </ul>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<script>
    new Vue({
        el: "#app",
        data: {
            userResources: [],
            userName: '',
            pageObj: {}

        },
        created() {
            this.getUserInfo();
            this.loadData();


        },
        methods: {
            loadData() {
                var vm = this;
                $.post("/resource/list", {}, function (response) {
                    if (response.code == 200) {
                        vm.pageObj = response.data;
                    }
                })
            },

            getUserInfo() {
                var vm = this;
                $.post("/userInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.userName = response.data.name
                        vm.userResources = response.data.role.resourceList

                    } else {
                        location.href = "/login.html"
                    }
                })
            },
            logout() {
                $.post("/logout", {}, function () {
                    location.href = "/login.html";
                })
            },
            addResource() {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '添加资源',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: '/resource/add.html',
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            deleteId(id) {
                var vm = this;
                var index = layer.confirm('您确定要删除吗？', {
                        btn: ['确定', '取消'] //按钮
                    },
                    function () {
                        $.post("/resource/deleteId", {id: id}, function (response) {
                            if (response.code == 200) {
                                vm.loadData();
                            } else {
                                layer.msg(response.msg);
                            }
                            layer.close(index)
                        })
                    }, function () {

                    });
            },
            updateResource(id) {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '资源修改',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'update.html?id=' + id,
                    end: function () {
                        vm.loadData();
                    }
                });
            }
        }
    });
</script>
</body>
</html>